﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2012/2/23 19:33:25
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sector7.Restaurant.EntityType;
using Sector7.Framework.Core;
using System.Collections;
using System.Data.SqlClient;
using Sector7.Restaurant.DAO.Utility;
using Sector7.Framework.Enums;
using Sector7.Framework.DB;

namespace Sector7.Restaurant.DAO.OleDB
{
    /// <summary>
    /// 销售单明细数据访问对象
    /// </summary>
    public class SalesBillItemDAO : ISalesBillItemDAO
    {
        #region Public functions
        public int Create(List<RS_SalesBillItem> items)
        {
            int result = -1;

            using (DBHelper opt = new DBHelper())
            {
                string sql =
                    @"INSERT INTO RS_SalesBillItem
                            (UnitId
                            ,UnitPrice
                            ,Number
                            ,BillId
                            ,MenuItemId)
                        VALUES
                            (@UnitId
                            ,@UnitPrice
                            ,@Number
                            ,@BillId
                            ,@MenuItemId)";

                result = opt.ExecuteBatchNonQuery(
                    sql,
                    new ArrayList(items),
                    "UnitId",
                    "UnitPrice",
                    "Number",
                    "BillId",
                    "MenuItemId");
            }

            return result;
        }

        /********************************************************/

        public List<RS_SalesBillItem> ReadAll(string billId, int itemStatus)
        {
            using (DBHelper opt = new DBHelper())
            {
                List<RS_SalesBillItem> billItems = new List<RS_SalesBillItem>();
                string sql =
                    "SELECT *                                               " +
                    "   FROM RS_SalesBillItem AS billItem                   " +
                    "   LEFT JOIN RS_MenuItem AS menuItem                   " +
                    "       ON billItem.MenuItemId = menuItem.MenuItemId    " +
                    "   LEFT JOIN RS_MetaData AS metaData                   " +
                    "       ON menuItem.MetaDataId = metaData.Id            " +
                    "   LEFT JOIN RS_Unit AS unit                           " +
                    "       ON menuItem.UnitId = unit.UnitId                " +
                    "   WHERE billItem.BillId = @BillId AND                 " +
                    "       billItem.Status <> @Status                      ";

                SqlDataReader reader = opt.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(billId, "BillId"),
                    DBUtil.CreateSqlParameter(itemStatus, "Status"));

                while (reader.Read())
                {
                    object menuItem = new RS_MenuItem();
                    DBUtil.ReadData(reader, ref menuItem);

                    object billItem = new RS_SalesBillItem();
                    DBUtil.ReadData(reader, ref billItem);
                    (billItem as RS_SalesBillItem).MenuItem = menuItem as RS_MenuItem;

                    object metaData = new RS_Metadata();
                    DBUtil.ReadData(reader, ref metaData);
                    (billItem as RS_SalesBillItem).MenuItem.Metadata = metaData as RS_Metadata;

                    object unit = new RS_Unit();
                    DBUtil.ReadData(reader, ref unit);
                    (billItem as RS_SalesBillItem).Unit = unit as RS_Unit;

                    billItems.Add(billItem as RS_SalesBillItem);
                }

                return billItems;
            }
        }

        public List<RS_SalesBillItem> ReadAll(string billId)
        {
            using (DBHelper opt = new DBHelper())
            {
                List<RS_SalesBillItem> billItems = new List<RS_SalesBillItem>();
                string sql =
                    "SELECT *                                               " +
                    "   FROM RS_SalesBillItem AS billItem                   " +
                    "   LEFT JOIN RS_MenuItem AS menuItem                   " +
                    "       ON billItem.MenuItemId = menuItem.MenuItemId    " +
                    "   LEFT JOIN RS_MetaData AS metaData                   " +
                    "       ON menuItem.MetaDataId = metaData.Id            " +
                    "   LEFT JOIN RS_Unit AS unit                           " +
                    "       ON menuItem.UnitId = unit.UnitId                " +
                    "   WHERE billItem.BillId = @BillId                     ";

                SqlDataReader reader = opt.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(billId, "BillId"));

                while (reader.Read())
                {
                    object menuItem = new RS_MenuItem();
                    DBUtil.ReadData(reader, ref menuItem);

                    object billItem = new RS_SalesBillItem();
                    DBUtil.ReadData(reader, ref billItem);
                    (billItem as RS_SalesBillItem).MenuItem = menuItem as RS_MenuItem;

                    object metaData = new RS_Metadata();
                    DBUtil.ReadData(reader, ref metaData);
                    (billItem as RS_SalesBillItem).MenuItem.Metadata = metaData as RS_Metadata;

                    object unit = new RS_Unit();
                    DBUtil.ReadData(reader, ref unit);
                    (billItem as RS_SalesBillItem).Unit = unit as RS_Unit;

                    billItems.Add(billItem as RS_SalesBillItem);
                }

                return billItems;
            }
        }

        public int Update(EntityDelta itemDelta)
        {
            using (DBHelper opt = new DBHelper())
            {
                string updateSql = DBUtil.CreateUpdateSql(itemDelta);
                return opt.ExecuteNonQuery(updateSql);
            }
        }

        public DBOptResultType Delete(string itemId)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
